package com.example.smart.ui.m

import android.text.Editable
import android.text.TextWatcher
import com.example.lib.constants.JumpConstants
import com.example.lib.extend.autoInt
import com.example.lib.extend.getIntentInt
import com.example.lib.extend.getIntentSerializable
import com.example.lib.extend.gone
import com.example.lib.extend.loadImage
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.extend.stringText
import com.example.lib.extend.visi
import com.example.lib.utils.AppUtils
import com.example.lib.utils.ToastUtils
import com.example.lib.utils.image_selector.ImageSelector
import com.example.lib.utils.statusbar.StatusBarUtils
import com.example.smart.R
import com.example.smart.base.ui.activity.BaseTitleBarActivity
import com.example.smart.data.AnimalData
import com.example.smart.databinding.ActivityAddAnimalMBinding
import com.example.smart.dialog.DateDialog
import com.example.smart.dialog.FySuccessDialog
import com.example.smart.dialog.SexDialog
import com.example.smart.room.bean.AnimalBean
import com.example.smart.room.factory.RoomFactory
import com.example.smart.utils.DisplayUtils
import com.example.smart.utils.handler.HandUtils

class MAddAnimalActivity : BaseTitleBarActivity<ActivityAddAnimalMBinding>() {
    private val mAnimalResponse = RoomFactory.createAnimalRepository()
    private var id = 0
    private var realPath = ""
    private val mDateDialog by lazy {
        DateDialog(this@MAddAnimalActivity)
    }

    private val mSexDialog by lazy {
        SexDialog(this@MAddAnimalActivity)
    }

    private val mFyDialog by lazy {
        FySuccessDialog(this@MAddAnimalActivity)
    }

    override fun layoutID() = R.layout.activity_add_animal_m

    override fun initView() {
        StatusBarUtils.setStateAndNavigationBar(this@MAddAnimalActivity, statusBarColor = com.example.lib.R.color.white)

        id = getIntentInt(JumpConstants.ID)
        if (id == 1) {
            binding.Ear.isEnabled = false
            binding.Birthday.isEnabled = false
            binding.Type.isEnabled = false
            binding.Sex.isEnabled = false
            binding.Weight.isEnabled = false
            binding.Source.isEnabled = false
            binding.DnDay.isEnabled = false
            binding.SexCount.isEnabled = false
            binding.AddImage.isEnabled = false
        } else if (id == 2) {
            binding.Ear.isEnabled = false
            binding.Birthday.isEnabled = false
            binding.Type.isEnabled = false
            binding.Sex.isEnabled = false
            binding.Weight.isEnabled = false
            binding.Source.isEnabled = false
            binding.DnDay.isEnabled = false
            binding.SexCount.isEnabled = false
            binding.AddImage.isEnabled = false
        }
    }

    override fun initData() {
        getIntentSerializable<AnimalData>(JumpConstants.Bean)?.let { bean ->
            binding.Ear.setText(bean.earNo)
            binding.Birthday.setText(bean.birthday)
            binding.Type.setText(bean.type)
            binding.Sex.setText(bean.sex)
            binding.Weight.setText(bean.weight)
            binding.Source.setText(bean.source)
            binding.DnDay.setText(bean.dnDay)
            binding.SexCount.setText(bean.sexCount)

            binding.Old.gone()
            binding.New.visi()
        }
    }

    override fun initListener() {

        binding.Ear.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
            }

            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
                binding.EarText.text = charSequence.toString().toCharArray().joinToString("\n").reversed()
            }

            override fun afterTextChanged(editable: Editable) {
            }
        })
        binding.EarText.setOnDoubleClickListener {
            AppUtils.showInputKey(this@MAddAnimalActivity, binding.Ear)
        }

        binding.Type.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
            }

            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
                binding.TypeText.text = charSequence.toString().toCharArray().joinToString("\n").reversed()
            }

            override fun afterTextChanged(editable: Editable) {
            }
        })
        binding.TypeText.setOnDoubleClickListener {
            AppUtils.showInputKey(this@MAddAnimalActivity, binding.Type)
        }

        binding.Weight.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
            }

            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
                binding.WeightText.text = charSequence.toString().toCharArray().joinToString("\n").reversed()
            }

            override fun afterTextChanged(editable: Editable) {
            }
        })
        binding.WeightText.setOnDoubleClickListener {
            AppUtils.showInputKey(this@MAddAnimalActivity, binding.Weight)
        }

        binding.Source.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
            }

            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
                binding.SourceText.text = charSequence.toString().toCharArray().joinToString("\n").reversed()
            }

            override fun afterTextChanged(editable: Editable) {
            }
        })
        binding.SourceText.setOnDoubleClickListener {
            AppUtils.showInputKey(this@MAddAnimalActivity, binding.Source)
        }

        binding.SexCount.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
            }

            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
                binding.SexCountText.text = charSequence.toString().toCharArray().joinToString("\n").reversed()
            }

            override fun afterTextChanged(editable: Editable) {
            }
        })
        binding.SexCountText.setOnDoubleClickListener {
            AppUtils.showInputKey(this@MAddAnimalActivity, binding.SexCount)
        }

        binding.AddImage.setOnDoubleClickListener {
            ImageSelector.takePhoto(this@MAddAnimalActivity) {
                val first = it.first()
                binding.TagImage.loadImage(first.realPath)
                realPath = first.realPath
            }
        }

        mDateDialog.onResult = { type, date ->
            if (type == 0) {
                binding.Birthday.text = date.toCharArray().joinToString("\n")
            } else {
                binding.DnDay.text = date.toCharArray().joinToString("\n")
            }
        }

        mSexDialog.onResult = {
            binding.Sex.text = it
        }

        binding.TagTry.setOnDoubleClickListener {
            DisplayUtils.toAddAnimalChoose(this@MAddAnimalActivity)
        }

        binding.Birthday.setOnDoubleClickListener {
            mDateDialog.show(0)
        }

        binding.DnDay.setOnDoubleClickListener {
            mDateDialog.show(1)
        }

        binding.Sex.setOnDoubleClickListener {
            mSexDialog.show()
        }

        binding.Confirm.setOnDoubleClickListener {
            if (id == 1) {
                mFyDialog.show()
            } else if (id == 2) {
                DisplayUtils.toYsDetails(this@MAddAnimalActivity)
            } else {
                val ear = binding.Ear.stringText()
                val birthday = binding.Birthday.stringText()
                val type = binding.Type.stringText()
                val sex = binding.Sex.stringText()
                val weight = binding.Weight.stringText()
                val source = binding.Source.stringText()
                val dnDay = binding.DnDay.stringText()
                val sexCount = binding.SexCount.stringText()
                if (ear.isEmpty() || birthday.isEmpty() || type.isEmpty() || sex.isEmpty() || weight.isEmpty() || source.isEmpty() || dnDay.isEmpty() || sexCount.isEmpty()) {
                    ToastUtils.showToast(getString(R.string.input_non_null))
                    return@setOnDoubleClickListener
                }

                mAnimalResponse.insert(AnimalBean(ear.replace("\n", "").autoInt(),
                    birthday.replace("\n", ""),
                    type.replace("\n", ""),
                    sex.replace("\n", ""),
                    weight.replace("\n", ""),
                    source.replace("\n", ""),
                    dnDay.replace("\n", ""),
                    sexCount.replace("\n", ""),
                    realPath))
                binding.Confirm.isEnabled = false
                ToastUtils.showToast(getString(R.string.add_success))
                HandUtils.postTask({
                    finish()
                }, 1000)
            }
        }
    }
}